Imports Microsoft.VisualBasic
Imports System
Imports System.Web.UI

Imports System.IO
Imports ICSharpCode.SharpZipLib.Zip.Compression.Streams

Namespace ViewStateCompression
    Public Class ZippedPage
        Inherits System.Web.UI.Page
        'Private _formatter As LosFormatter = New LosFormatter()

        'Public Function Compress(ByVal bytes As Byte()) As Byte()
        '    Dim memory As MemoryStream = New MemoryStream()
        '    Dim stream As Stream = New DeflaterOutputStream(memory, New ICSharpCode.SharpZipLib.Zip.Compression.Deflater(ICSharpCode.SharpZipLib.Zip.Compression.Deflater.BEST_COMPRESSION), 131072)
        '    stream.Write(bytes, 0, bytes.Length)
        '    stream.Close()
        '    Return memory.ToArray()
        'End Function

        'Public Function Decompress(ByVal bytes As Byte()) As Byte()
        '    Dim stream As Stream = New InflaterInputStream(New MemoryStream(bytes))
        '    Dim memory As MemoryStream = New MemoryStream()
        '    Dim totalLength As Integer = 0
        '    Dim writeData(4096) As Byte '() = New array (4096) of Byte
        '    Dim size As Integer
        '    size = 0
        '    While (True)
        '        size = stream.Read(writeData, 0, writeData.Length)
        '        If (size > 0) Then
        '            totalLength += size
        '            memory.Write(writeData, 0, size)
        '        Else
        '            Exit While
        '        End If
        '    End While
        '    stream.Close()
        '    Return memory.ToArray()
        'End Function

        'Protected Overrides Sub SavePageStateToPersistenceMedium(ByVal viewState As Object)
        '    Dim format As LosFormatter = New LosFormatter()
        '    Dim writer As StringWriter = New StringWriter()
        '    format.Serialize(writer, viewState)
        '    Dim viewStateStr As String = writer.ToString()
        '    Dim bytes As Byte() = System.Convert.FromBase64String(viewStateStr)
        '    bytes = Compress(bytes)
        '    Dim vStateStr As String = System.Convert.ToBase64String(bytes)
        '    ClientScript.RegisterHiddenField("__VSTATE", vStateStr)
        'End Sub

        'Protected Overrides Function LoadPageStateFromPersistenceMedium() As Object
        '    Dim vState As String = Request.Form("__VSTATE")
        '    Dim bytes As Byte() = System.Convert.FromBase64String(vState)
        '    bytes = Decompress(bytes)
        '    Dim format As LosFormatter = New LosFormatter()
        '    Return format.Deserialize(System.Convert.ToBase64String(bytes))
        'End Function

        Public Function GetFindSQLString(ByVal strSource As String, ByVal strType As String) As String
            Dim strTemp As String
            Dim intResult As Integer
            Dim dtResult As DateTime

            strTemp = ""
            Select Case strType
                Case "String"
                    strTemp = Replace(strSource, " ", "")
                Case "Number"
                    If Decimal.TryParse(strSource, intResult) Then
                        strTemp = intResult.ToString
                    End If
                Case "DateTime"
                    If DateTime.TryParse(strSource, dtResult) Then
                        strTemp = dtResult.ToString("dd\.MM\.yyyy HH:mm")
                    End If
            End Select
            GetFindSQLString = strTemp
        End Function

        Public Sub SetConnectionStringForDataSources(ByRef PageLink As Control)
            Dim strConnectionString As String
            Dim ctrlRef As Control
            'Session("uid") = "sa"
            'Session("pwd") = "s17l35d3roman"
            strConnectionString = ConfigurationManager.ConnectionStrings("DatabaseConnection").ConnectionString & "uid=" & Session("uid").ToString & ";pwd=" & Session("pwd").ToString
            For Each ctrlRef In PageLink.Controls
                If TypeOf ctrlRef Is SqlDataSource Then
                    If CType(ctrlRef, SqlDataSource).ConnectionString <> strConnectionString Then
                        CType(ctrlRef, SqlDataSource).ConnectionString = strConnectionString
                        'CType(ctrlRef, SqlDataSource).DataBind()
                    End If
                Else
                    SetConnectionStringForDataSources(ctrlRef)
                End If
            Next ctrlRef
        End Sub

    End Class
End Namespace